/**
 * 
 */
package com.faxsun.core.persist.entity;

import java.util.Comparator;
import java.util.Date;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

/**
 * @author langhsu
 * 
 */
@Entity
@Table(name = "fx_posts")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Indexed(index = "posts")
@Analyzer(impl = FaxsunAnalyzer.class)
public class PostPO {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@DocumentId
	@Column(name="post_id")
	private long id;

	private int type;

	@Field(name = "title", index = Index.YES, analyze = Analyze.YES, store = Store.YES)
	@Analyzer(impl = FaxsunAnalyzer.class)
	private String title; // 标题
	
	@Lob
	@Basic(fetch = FetchType.EAGER) 
	@Type(type="text")
	@Field(name = "summary", index = Index.YES, analyze = Analyze.YES, store = Store.YES)
	@Analyzer(impl = FaxsunAnalyzer.class)
	private String summary; // 摘要

	@Field(name = "tags", index = Index.YES, analyze = Analyze.YES, store = Store.YES)
	@Analyzer(impl = FaxsunAnalyzer.class)
	private String tags;
	
	@Lob
	@Basic(fetch = FetchType.LAZY) 
	@Type(type="text")
	@Field(name = "content", index = Index.YES, analyze = Analyze.YES, store = Store.NO)
	@Analyzer(impl = FaxsunAnalyzer.class)
	private String content; // 内容
	
	@Lob
	@Basic(fetch = FetchType.LAZY)
	@Type(type="text")
	private String markdown; // markdown 内容
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "snapshot_id")
	private AttachPO snapshot;

	@Temporal(value = TemporalType.TIMESTAMP)
	private Date created;

	@Temporal(value = TemporalType.TIMESTAMP)
	private Date updated;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "author_id")
	private UserPO author;
	
	@ManyToMany(fetch=FetchType.LAZY,cascade = {CascadeType.REFRESH})
	@JoinTable(name="fx_post_tag_ref", 
				joinColumns={@JoinColumn(name="post_id")}, 
				inverseJoinColumns={@JoinColumn(name="tag_id")})
	private Set<TagPO> tagSets;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "tag_id")
	private TagPO defaultTag;

	private int featured; // 是否推荐
	private int hearts; // 喜欢
	private int comments;//评论数
	private int views; // 阅读
	private int status; //状态，未来使用。0，待审核;1,审核通过,默认为1;2,删除
	
	@Column(name="source_website")
	private String sourceWebsite;
	
	@Column(name="source_url")
	private String sourceUrl;
	
	@Transient
	private String searchTitle;    
    @Transient
    private String searchTags;    
    @Transient
    private String searchSummary;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public AttachPO getSnapshot() {
		return snapshot;
	}

	public void setSnapshot(AttachPO snapshot) {
		this.snapshot = snapshot;
	}

	public String getTags() {
		if(tags==null){
			tags="";
		}
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Date getUpdated() {
		return updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}

	public UserPO getAuthor() {
		return author;
	}

	public void setAuthor(UserPO author) {
		this.author = author;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getFeatured() {
		return featured;
	}

	public void setFeatured(int featured) {
		this.featured = featured;
	}

	public int getHearts() {
		return hearts;
	}

	public void setHearts(int hearts) {
		this.hearts = hearts;
	}

	public int getComments() {
		return comments;
	}

	public void setComments(int comments) {
		this.comments = comments;
	}

	public int getViews() {
		return views;
	}

	public void setViews(int views) {
		this.views = views;
	}

	public String getMarkdown() {
		return markdown;
	}

	public void setMarkdown(String markdown) {
		this.markdown = markdown;
	}

	public Set<TagPO> getTagSets() {
		return tagSets;
	}

	public void setTagSets(Set<TagPO> tagSets) {
		this.tagSets = tagSets;
	}

	public TagPO getDefaultTag() {
		return defaultTag;
	}

	public void setDefaultTag(TagPO defaultTag) {
		this.defaultTag = defaultTag;
	}

	public String getSourceWebsite() {
		return sourceWebsite;
	}

	public void setSourceWebsite(String sourceWebsite) {
		this.sourceWebsite = sourceWebsite;
	}

	public String getSourceUrl() {
		return sourceUrl;
	}

	public void setSourceUrl(String sourceUrl) {
		this.sourceUrl = sourceUrl;
	}

    public String getSearchTitle() {
        return searchTitle;
    }

    public void setSearchTitle(String searchTitle) {
        this.searchTitle = searchTitle;
    }

    public String getSearchTags() {
        return searchTags;
    }

    public void setSearchTags(String searchTags) {
        this.searchTags = searchTags;
    }

    public String getSearchSummary() {
        return searchSummary;
    }

    public void setSearchSummary(String searchSummary) {
        this.searchSummary = searchSummary;
    }
    
    public static class Comparators {

        /**
         * Id descending order
         */
        public static Comparator<PostPO> DFLT_ID = new Comparator<PostPO>() {
            @Override
            public int compare(PostPO o1, PostPO o2) {
                int ret = (int) (o2.id - o1.id);//id desc
                return ret;
            }
        };
    }  
	
	
}
